/**
 * Created by hsia on 2017/4/12.
 * 日夜组合收费
 */
var dayNightChargeJs = function(targetObj){
    this.sysUtils = new kendoUtils();
    this.targetObj = targetObj;
    this.init();
    return this;
};

var standardBinfo = function(id, $list) {
    if (id == undefined || !id) {
        this.id = "d-" + new Date().getTime();
    } else {
        this.id = id;
    }
    this.$list = $list;
    return this;
}
standardBinfo.prototype = {
    addToList : function($p) {
        $p.createTr(this.$list.find("tbody"));
    }
}
dayNightChargeJs.prototype={
    init:function(){
        var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
        var bindFunctionArray = [
            {fName:"savechargeInfo",fs:this.savechargeInfo},
            {fName:"cancel",fs:function(){$("#"+tempchargeList.monthChargeDivId).data("kendoWindow").close();}},
            {fName:"createRow",fs:this.createRow},
            {fName:"deleteRow",fs:this.deleteRow}];
        this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
        this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
        this.targetObj.find(":radio[name='dailyCap']").click(this.clickRadio);
        var payMethod = Utils.getModelValueByModelKey(BASIS.CHARGE_NIGHT_PAY_METHOD.ONCE);
        Utils.createDataDictDropDownList([{div:this.targetObj.find("#payMethod"),parentModelKey:BASIS.CHARGE_NIGHT_PAY_METHOD.ROOT,select:payMethod,change:this.payMethodChange}],this);
        this.loadChargeInfo()
    },
    //保存收费标准信息
    savechargeInfo:function(e){
        var that =  $(e.target).data("that");
        if(validityForm(that.targetObj)){
            var chargeInfo = that.sysUtils.getBindDocJson();
            var url = baseApiPath+"chargeStandard/save?isEdit=" + tempchargeList.isEdit;
            var sendJson=chargeInfo;
            for(var o in chargeInfo){
                if(o.indexOf('_') !=-1){
                    var strArray = o.substring(o.indexOf('_')+1);
                    var name = o.substring(0, o.indexOf('_'));
                    var jsonA = {};
                    jsonA[name] = that.recursionObjectName(strArray,chargeInfo[o]) ;
                    delete chargeInfo[o];
                    var sendJson =$.extend(true,chargeInfo,jsonA);
                }
            }
            //封装切换不同收费方式数据
            var payMethodDownList = $('#payMethod').data('kendoDropDownList');
            var flag = payMethodDownList.dataItem(payMethodDownList.select()).modelValue == '1' ? true : false;
            if(flag){//按时段
                delete sendJson['chargeStandardInfos']['night']['payMoney'];
            }else{//按次
                delete sendJson['chargeStandardInfos']['night']['firstTime'];
                delete sendJson['chargeStandardInfos']['night']['firstChargeMoney'];
                delete sendJson['chargeStandardInfos']['night']['intervalChargingTime'];
                delete sendJson['chargeStandardInfos']['night']['intervalChargingMoney'];
            }

            //封装封顶组别数据
            var jsonE = {chargeStandardInfos:{allDay:{}}};
            that.targetObj.find(":radio[name='dailyCap']").each(function () {
                if(this.checked){
                    var id = this.id;
                    if(id=='' || id == undefined){
                        jsonE.chargeStandardInfos.allDay["dailyCapMoney"] = '-1';
                        jsonE.chargeStandardInfos.allDay["dailyCapTime"] = '-1';
                    }else if(id == 'Money'){
                        jsonE.chargeStandardInfos.allDay["dailyCapTime"] = '-1';
                    }else if(id == 'Time'){
                        jsonE.chargeStandardInfos.allDay["dailyCapMoney"] = '-1';
                    }
                    sendJson = $.extend(true,sendJson,jsonE);
                }
            });
            var flag = true;
            if(!tempchargeList.isEdit){
                sendJson["chargeType"]=1;
                sendJson["chargeParamCode"]='dayNight';
            }
            if(!flag){
                return;
            };
            JsonpAjax(url,JSON.stringify(sendJson),"json",function(result){
                $("#"+tempchargeList.monthChargeDivId).data("kendoWindow").close();
                tempchargeList.findGridData();
            });
        }
    },
    recursionObjectName : function (strArray,val) {
        if (strArray.indexOf('_') == -1){
            var jsonC = {};
            jsonC[strArray]=val;
            return jsonC;
        }
        var name = strArray.substring(0, strArray.indexOf('_'));
        strArray = strArray.substring(strArray.indexOf('_')+1);
        var jsonB = {};
        jsonB[name] = this.recursionObjectName(strArray,val);
        return jsonB;
    },
    loadChargeInfo : function(e) {
        var that = this;
        if (!isEmpty(e)) {
            that = $(e.target).data("that");
        }
        var cumulativeWay = this.targetObj.find("#cumulativeWay");
        var wayA = Utils.getModelValueByModelKey(BASIS.CUMULATIVE_WAY.CUMULATIVE_WAY_A);
        Utils.createDataDictDropDownList([{div:cumulativeWay,parentModelKey:BASIS.CUMULATIVE_WAY.ROOT,select:wayA}],this);
        var freeOvertimeBiling = this.targetObj.find("#freeOvertimeBiling");
        var biling = Utils.getModelValueByModelKey(BASIS.BOTT_LEVEL_ROOT.BOTT_LEVEL_N);
        Utils.createDataDictDropDownList([{div:freeOvertimeBiling,parentModelKey:BASIS.BOTT_LEVEL_ROOT.ROOT,select:biling}],this);
        var freePreferentialWay = this.targetObj.find("#freePreferentialWay");
        var ferentialWay = Utils.getModelValueByModelKey(BASIS.FREE_PREFERENTIAL_WAY.FREE_PREFERENTIAL_WAY_D);
        Utils.createDataDictDropDownList([{div:freePreferentialWay,parentModelKey:BASIS.FREE_PREFERENTIAL_WAY.ROOT,select:ferentialWay}],this);
        if(null == tempchargeList.editResult) return; //新增啥都不需要做
        this.sysUtils.setVariantBindFieldValue(tempchargeList.editResult);
        that.showOff();
        that.chargeStandardInfos = tempchargeList.editResult.chargeStandardInfos;
        var basic = that.chargeStandardInfos.allDay;
        var money=basic.dailyCapMoney,time=basic.dailyCapTime;
        if(money=='-1'&&time=='-1'){
            that.targetObj.find(":radio[name='dailyCap']")[0].checked = true;
            that.targetObj.find('#dailyCapMoney').prop('disabled',true);
            that.targetObj.find('#dailyCapTime').prop('disabled',true);
        }else if(money!='-1'&&time=='-1'){
            that.targetObj.find(":radio[name='dailyCap']")[1].checked = true;
            that.targetObj.find('#dailyCapMoney').prop('disabled',false);
        }else if(money=='-1'&&time!='-1'){
            that.targetObj.find(":radio[name='dailyCap']")[2].checked = true;
            that.targetObj.find('#dailyCapTime').prop('disabled',false);
        }

    },
    showOff:function () {
        var payMethodDownList = $('#payMethod').data('kendoDropDownList');
        var flag = payMethodDownList.dataItem(payMethodDownList.select()).modelValue == '1' ? true : false;
        var one = this.targetObj.find('#one');
        if(flag){
        	one.hide();
        	one.find("input").attr("disabled","disabled");
            $('#two').show();
        }else{
        	one.show();
        	one.find("input").removeAttr("disabled");
            $('#two').hide();
        }
    },
    payMethodChange:function () {
        var flag = this.dataItem(this.select()).modelValue == '1' ? true : false;
        var one = dayNightCharge.targetObj.find('#one');
        if(flag){
        	one.hide();
        	one.find("input").attr("disabled","disabled");
            $('#two').show();
        }else{
        	one.show();
        	one.find("input").removeAttr("disabled");
            $('#two').hide();
        }
    },
    clickRadio:function (e) {
        var id =this.id;
        if(id=='' || id == undefined){
            $('#dailyCapMoney').prop('disabled',true);
            $('#dailyCapMoney').prop('required',false);
            $('#dailyCapTime').prop('disabled',true);
            $('#dailyCapTime').prop('required',false);
        }else if(id == 'Money'){
            $('#dailyCapMoney').prop('disabled',false);
            $('#dailyCapMoney').prop('required',true);
            $('#dailyCapTime').prop('disabled',true);
            $('#dailyCapTime').prop('required',false);
        }else if(id == 'Time'){
            $('#dailyCapTime').prop('disabled',false);
            $('#dailyCapTime').prop('required',true);
            $('#dailyCapMoney').prop('disabled',true);
            $('#dailyCapMoney').prop('required',false);
        }
    }
}